<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/12/28 21:41
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */

use Wechat\Loader;
use Wechat\WechatReceive;

class Listen extends CI_Controller
{

    private $options = [];

    /**
     * @var Wechat\WechatReceive
     */
    private $wechat = null;

    public function __construct()
    {
        parent::__construct();
        $this->options = $this->config->item('wechat');
    }


    public function valid()
    {
        Loader::config($this->options);
        $this->wechat = Loader::get('Receive');
        $type = $this->wechat->getRev()->getRevType();

        $openId = $this->wechat->getRevFrom();
        switch($type) {
            // 接收到的文本信息
            case WechatReceive::MSGTYPE_TEXT:
                $this->wechat->text('欢迎关注')->reply();
                break;

            // 接收到的事件消息
            case WechatReceive::MSGTYPE_EVENT:
                $event = $this->wechat->getRevEvent();
                $this->_event($event, $openId);
                break;
        }

        /*
        $wechatUser = Loader::get('user');
        $userInfo = $wechatUser->getUserInfo($openId);
        if (!$userInfo) {
            $userInfo = $wechatUser->errMsg . ' ' . $wechatUser->errCode;
        }


        file_put_contents('/tmp/rev.log', var_export($type, true) . PHP_EOL, FILE_APPEND);
        file_put_contents('/tmp/rev.log', var_export($openId, true) . PHP_EOL, FILE_APPEND);
        file_put_contents('/tmp/rev.log', var_export($userInfo, true) . PHP_EOL, FILE_APPEND);
        */
        // echo $_GET['echostr'];
    }

    /**
     * 拦截事件消息，执行不同业务逻辑
     *
     * @param $event
     * @param $openid
     */
    private function _event($event, $openid)
    {
        $sceneId = $this->wechat->getRevSceneId();

        if (!empty($sceneId)) {
            // 带场景的扫描,推荐关注

        }

        $wechatUser = Loader::get('user');
        $userInfo = $wechatUser->getUserInfo($openid);
        $userInfo = $this->_formatUserInfo($userInfo);

        switch ($event['event']) {
            // 关注
            case 'subscribe':
                $this->_subscribe($userInfo);
                break;
            // 取消关注
            case 'unsubscribe':
                $this->_subscribe($userInfo, false);
                break;
            // 扫码
            case 'SCAN':
                $this->_subscribe($userInfo, false);
                break;
            // 点击事件
            case 'CLICK':
                break;
        }
    }

    /**
     * 关注与被关注
     *
     * @param $userInfo
     * @param bool $subscribe
     */
    private function _subscribe($userInfo, $subscribe = true)
    {
        // 取消时关注不更新关注时间
        if (!$subscribe) {
            if (isset($userInfo['subscribe_time'])) {
                unset($userInfo['subscribe_time']);
            }
        }

        // 存在更新，不存在添加
        $userModel = new UserModel();
        $userModel->createUpdate($userInfo);
    }

    /**
     * 格式化用户信息
     *
     * @param $userInfo
     */
    private function _formatUserInfo($userInfo)
    {
        if (!empty($userInfo)) {
            $userInfo['subscribe_time'] = date('Y-m-d H:i:s');

            if (isset($userInfo['tagid_list'])) {
                unset($userInfo['tagid_list']);
            }
        }

        return $userInfo;
    }
}
